package com.fanqi.nxplayer;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.fanqi.nxplayer.camera.Camera2Activity;
import com.fanqi.nxplayer.camera.CameraActivity;
import com.fanqi.viewstub.ViewStubActivity;
import com.tencent.mmkv.MMKV;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.lang.reflect.Method;
import java.util.UUID;
import java.util.concurrent.Executors;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.BackpressureStrategy;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableEmitter;
import io.reactivex.rxjava3.core.FlowableOnSubscribe;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getCanonicalName();

    private SurfaceView avSurfaceView;

    private MMKV mmkv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        avSurfaceView = findViewById(R.id.avView);
        // Example of a call to a native method
        Thread.currentThread();

        Executors.newFixedThreadPool(3);


        //TouchSlop（系统常量）表示最小移动阈值，默认值一般为8dp。这个相信都能明白，意思就是如果你滑动小于这个值的话系统就会认为你这不属于滑动事件。这里需要注意，有的手机厂商为了“提高用户体验”，重新设置了这个值的大小
        Log.d(TAG, "onCreate: " + ViewConfiguration.get(this).getScaledTouchSlop());

        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Throwable {
                emitter.onNext(20);
            }
        }).map(new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) throws Throwable {
                return ageToStr(integer);
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Throwable {
                //update android UI
                update(s);
            }
        });

        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Throwable {
                emitter.onNext(18);
            }
        }).flatMap(new Function<Integer, ObservableSource<String>>() {
            @Override
            public ObservableSource<String> apply(Integer integer) throws Throwable {

                return null;
            }
        }).doOnNext(new Consumer<String>() {
            @Override
            public void accept(String s) throws Throwable {

            }
        });


        //背压式的 被观察者--》观察者
        //Flowable -->  Subscriber
        Flowable.create(new FlowableOnSubscribe<Integer>() {

            @Override
            public void subscribe(@NonNull FlowableEmitter<Integer> emitter) throws Throwable {
                emitter.onNext(100);
            }
        }, BackpressureStrategy.ERROR).subscribe(new Subscriber<Integer>() {
            @Override
            public void onSubscribe(Subscription s) {

            }

            @Override
            public void onNext(Integer integer) {

            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {

            }
        });

        //mmkv
        mmkv = MMKV.defaultMMKV();
        mmkv.encode("strKey", "fanq");

        mmkv.putString("putopt","尝试put");

        MMKV kv = MMKV.mmkvWithID("app");
        kv.encode("mode", 1);

        mmkv.encode("strKey", 1);
        mmkv.encode("strKey", "我");
        mmkv.encode("strKey", "是");
        mmkv.encode("strKey", "启");

        Log.d(TAG, "onCreate: MMKV putopt="+mmkv.getString("putopt","null"));


        TextView tv = findViewById(R.id.serial);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            tv.setText(mmkv.decodeString("strKey"));
        }

    }

    private String ageToStr(int age) {
        return "Wo shi shui ,wo jin nian age=" + age;
    }

    private void update(String s) {
        Log.d(TAG, "update: " + s);
    }


    public void startCamera(View view) {
        startActivity(new Intent(MainActivity.this, CameraActivity.class));
    }

    public void startCamera2(View view) {
        startActivity(new Intent(MainActivity.this, Camera2Activity.class));
    }

    public String setSerialNumber() {
        String serial = null;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                serial = Build.getSerial();
            } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
                serial = Build.SERIAL;
            } else {
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method get = c.getMethod("get", String.class);
                serial = (String) get.invoke(c, "ro.serialno");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "获取设备序列号失败");
        }
        return serial;
    }

    public static String getUUID() {

        String serial = null;

        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +

                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +

                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +

                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +

                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +

                Build.USER.length() % 10; //13 位
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                serial = Build.getSerial();
            } else {
                serial = Build.SERIAL;
            }
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
        //使用硬件信息拼凑出来的15位号码
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }

    /**
     * onWindowFocusChanged来获取主按键的具体位置和宽高的,而在其他标准生命周期的接口中调用都是获取不到的，比如在onResume，onStart中都获取不到信息。
     * 这个onWindowFocusChanged指的是这个Activity得到或者失去焦点的时候 就会call。。
     * 也就是说 如果你想要做一个Activity一加载完毕，就触发什么的话 完全可以用这个
     * @param hasFocus
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    }

    public void viewStub(View view) {
        startActivity(new Intent(MainActivity.this, ViewStubActivity.class));
    }
}
